﻿<?php 
$arrColMap = array();
$arrColMap['name'] = array('bb'=>'tenbb', 'dt'=>'tendt', 'sm'=>'chude');
$arrColMap['summary'] = array('bb'=>'tomtat_bb', 'dt'=>'tomtat_dt', 'sm'=>'tomtat');


function getItemQuery($cat,$catdt='')
{
	$sql = '';
	$selectField = '';
	$cond = '';
	if ($catdt == '') {
		$summary_text_length = MAIN_SUMMARY_TEXT_LENGTH;
	} else {
		$summary_text_length = 2147483647; // max int value => no limit
	}
	if ($cat == 'bb')
	{
		if ($catdt != '') {
			$cond = " WHERE bb.id = $catdt ";
		}
		$sql = " SELECT bb.id, bb.tenbb as ten, SUBSTR(bb.tomtat_bb, 1, $summary_text_length) as tomtat, bb.ngay_dang, GROUP_CONCAT(DISTINCT hotentg) cac_tg,
				GROUP_CONCAT(DISTINCT tg.id) as cac_tgid, tc.tentc, cttc.so, cttc.ngayra
				FROM baibao bb JOIN tacgia_baibao tb ON bb.id = tb.bb_id 
				JOIN tacgia tg ON tg.id = tb.tg_id 
				JOIN chitiet_tapchi cttc ON bb.cttc_id = cttc.id 
				JOIN tapchi tc ON tc.id = cttc.tc_id 
				$cond 
				GROUP BY bb.id 
				ORDER BY ngay_dang DESC ";
		//echo "<br /><br /><br /><br /><br />".$sql;
	}
	else if ($cat == 'dt')
	{
		if ($catdt != '') {
			$cond = " WHERE dt.id = $catdt ";
		}
		$sql = "SELECT dt.id, dt.tendt as ten, SUBSTR(dt.tomtat_dt, 1, $summary_text_length) as tomtat, dt.ngay_dang,
					GROUP_CONCAT(DISTINCT hotentg) cac_tg,
					GROUP_CONCAT(DISTINCT tg.id) as cac_tgid 
				FROM detai dt JOIN tacgia_detai td ON dt.id = td.dt_id
					JOIN tacgia tg ON tg.id = td.tg_id
				$cond
				GROUP BY dt.id 
				ORDER BY ngay_dang DESC";

	}

	return $sql;
}

function getTomtat($tomtat, $summary_text_length = SUMMARY_TEXT_LENGTH)
{
	if (strlen($tomtat) > ($summary_text_length - 20))
	{
		$lpos = strrpos($tomtat, " ", $summary_text_length - 50);
		if ($lpos != false) {
			return substr($tomtat, 0, $lpos)."...";
		}
	}
	return $tomtat;
}

function getTitle($cat, $subcat='')
{
	$title = 'Default title';
	switch ($cat)
	{
		case 'bb':
			if ($subcat == '') { 
				$title = 'Bài báo';
			} else {
				$sql = "SELECT tenloai FROM loaitapchi ltc WHERE id = $subcat ";
				$result = mysql_query($sql) or die('Không lấy được loại bài báo. ' . mysql_error());
				$row = mysql_fetch_assoc($result);
				extract($row);
				$title = "Bài báo " . $tenloai;
			}
			break;
		case 'dt':
			if ($subcat == '') {
				$title = 'Đề tài Khoa học';
			} else {
				$sql = "SELECT tencap FROM capthuchien cth WHERE id = $subcat ";
				$result = mysql_query($sql) or die('Không lấy được cấp thực hiện. ' . mysql_error());
				$row = mysql_fetch_assoc($result);
				extract($row);
				$title = $tencap;
			}
			break;
		case 'ht':
			if ($subcat == '') $title = 'Thông tin Hội thảo';
			break;
		case 'kyht':
			if ($subcat == '') $title = 'Kỷ yếu Hội thảo';
			break;
		case 'tcnb':
			if ($subcat == '') {
				$title = 'Tạp chí Nội bộ';
			} else {
				$sql = "SELECT CONCAT_WS(' ',tentc, 'số', so) as tentc  FROM chitiet_tapchi cttc JOIN tapchi tc ON cttc.tc_id = tc.id WHERE cttc.id = $subcat ";
				$result = mysql_query($sql) or die('Không lấy được tên tạp chí. ' . mysql_error());
				$row = mysql_fetch_assoc($result);
				extract($row);
				$title = $tentc;
			}
			break;
		case 'gttl':
			$title = 'Giáo trình & Tài liệu';
			break;
		case 'hpsp':
			$title = 'Họa phẩm - Sản phẩm';
		break;
		default:
			break;
	}
	
	return $title;
}

function getAllArticlesByAuthorId($author_id)
{
	$summary_text_length = SUMMARY_TEXT_LENGTH;
	$baibaoSql = " SELECT bb.id, bb.tenbb as ten, SUBSTR(bb.tomtat_bb, 1, $summary_text_length) as tomtat, bb.ngay_dang, GROUP_CONCAT(DISTINCT hotentg) cac_tg,
			GROUP_CONCAT(DISTINCT tg.id) as cac_tgid FROM baibao bb JOIN tacgia_baibao tb ON bb.id = tb.bb_id 
			JOIN tacgia tg ON tg.id = tb.tg_id 
			JOIN tacgia_baibao tb2 ON tb2.bb_id = bb.id 
			WHERE tb2.tg_id = $author_id
				GROUP BY bb.id ";
	$detaiSql = " SELECT dt.id, dt.tendt as ten, SUBSTR(dt.tomtat_dt, 1, $summary_text_length) as tomtat, dt.ngay_dang,
					GROUP_CONCAT(DISTINCT hotentg) cac_tg,
					GROUP_CONCAT(DISTINCT tg.id) as cac_tgid 
				  FROM detai dt JOIN tacgia_detai td ON dt.id = td.dt_id
					JOIN tacgia tg ON tg.id = td.tg_id
					JOIN tacgia_detai td2 ON td2.dt_id = dt.id
				  WHERE td2.tg_id = $author_id
					GROUP BY dt.id ";
	$sql = " ($baibaoSql) UNION ($detaiSql) ";
	
	return $sql;					
}

 ?>
